package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.common.ShfConstant;
import com.atguigu.entity.*;
import com.atguigu.entity.vo.HouseQueryVo;
import com.atguigu.entity.vo.HouseVo;
import com.atguigu.result.HouseTypeEnum;
import com.atguigu.result.Result;
import com.atguigu.service.*;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 翟文海
 * @Date: 2022/10/30/030 16:54
 * @Version: 1.0
 * @Description:
 **/
@RestController
@RequestMapping("/house")
public class HouseController {

    @Reference
    private HouseService houseService;

    @Reference
    private CommunityService communityService;

    @Reference
    private HouseBrokerService houseBrokerService;

    @Reference
    private UserFollowService userFollowService;

    @Reference
    private HouseImageService houseImageService;

    @RequestMapping("/list/{pageNum}/{pageSize}")
    public Result list(@PathVariable("pageNum") Integer pageNum,
                       @PathVariable("pageSize") Integer pageSize,
                       @RequestBody HouseQueryVo houseQueryVo) {
        PageInfo<HouseVo> pageInfo = houseService.findListPage(pageNum, pageSize, houseQueryVo);
        return Result.ok(pageInfo);
    }

    @GetMapping("/info/{id}")
    public Result info(@PathVariable("id") Long id, HttpSession session) {
        boolean isFollow = false;
        House house = houseService.getById(id);
        Community community = communityService.getById(house.getCommunityId());
        List<HouseBroker> houseBrokerList = houseBrokerService.findHouseBrokerListByHouseId(id);
        List<HouseImage> houseImage1List = houseImageService.findHouseImageList(id, HouseTypeEnum.HOUSE_TYPE_RESOURCE.getType());
        Map<String,Object> map = new HashMap<>();
        map.put("house",house);
        map.put("community",community);
        map.put("houseBrokerList",houseBrokerList);
        map.put("houseImage1List", houseImage1List);
        UserInfo userInfo = (UserInfo) session.getAttribute(ShfConstant.UserConstant.USER_INFO);
        if (userInfo != null) {
            UserFollow userFollow = userFollowService.getByHouseIdAndUserId(userInfo.getId(), id);
            isFollow = (userFollow!=null&&userFollow.getIsDeleted()==0);
        }
        map.put("isFollow",isFollow);
        return Result.ok(map);
    }
}
